cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/* Effects by sector and by age */
capture program drop wagesog
program define wagesog
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* create quartiles */
gen age2534 =(age>=25 & age<=34) if btime==1
gen age3544 =(age>=35 & age<=44) if btime==1
gen age4550 =(age>=45 & age<=50) if btime==1
replace age2534=L.age2534 if btime==2
replace age3544=L.age3544 if btime==2
replace age4550=L.age4550 if btime==2

gen gtrad=trad if btime==1
replace gtrad=L.trad if btime==2
gen gntrad=ntrad if btime==1
replace gntrad=L.ntrad if btime==2
gen gconstbc=constbc if btime==1
replace gconstbc=L.constbc if btime==2

replace gntrad=0 if gconstbc==1

gen age2534nq=age2534*bcnq
gen age3544nq=age3544*bcnq
gen age4550nq=age4550*bcnq

gen age2534q=age2534*bcq
gen age3544q=age3544*bcq
gen age4550q=age4550*bcq


foreach OCC in gtrad gntrad gconstbc age2534 age3544 age4550{
gen dimm`OCC'=dimm*`OCC'
gen dimm75`OCC'=dimm75*`OCC'
}

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

end

wagesog, occp(bc) 

/* Age */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store bcb1

ivreg2 drlsnd (dimm dimmgtrad dimmgconstbc = dimm75 dimm75gtrad dimm75gconstbc) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store bcb3

estout bcb1 bcb3, cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

wagesog, occp(bcnq) 

/* Age */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store bcnqb1

ivreg2 drlsnd (dimm dimmgtrad dimmgconstbc = dimm75 dimm75gtrad dimm75gconstbc) Y_* [aweight = iw2], partial(Y_* ) cl(ze)
estimates store bcnqb3

estout bcb1 bcb3 bcnqb1 bcnqb3, cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab bcb1 bcb3 bcnqb1 bcnqb3 ///
using "C:\Users\Public\Documents\Tab21\TableA9.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
